package gamesys.games.particlesystem.graphics
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;

	/**
	 * @author robertthurston
	 */
	public class BitmapCanvas extends Bitmap
	{
		private var _point : Point;
		private var _matrix : Matrix;
		private var _zeroPoint : Point;
		private var _lineSprite : Sprite;

		public function BitmapCanvas(width : Number, height : Number)
		{
			super(new BitmapData(width, height, true, 0x00000000), "auto", true);

			_point = new Point();
			_matrix = new Matrix();
			_zeroPoint = new Point();
			_lineSprite = new Sprite();
		}

		public function draw(source : BitmapData, x : Number, y : Number) : void
		{
			_matrix.tx = x;
			_matrix.ty = y;

			bitmapData.draw(source, _matrix);
			smoothing = true;
		}

		public function copyPixels(source : BitmapData, x : Number, y : Number, alphaBitmapData : BitmapData = null) : void
		{
			_point.x = x;
			_point.y = y;

			bitmapData.copyPixels(source, source.rect, _point, alphaBitmapData, null, true);

			smoothing = true;
		}

		public function drawLine(prevX : Number, prevY : Number, x : Number, y : Number, alphaBitmapData : BitmapData = null) : void
		{
			_point.x = x;
			_point.y = y;

			_lineSprite.graphics.clear();
			_lineSprite.graphics.lineStyle(1, 0x111111, .2);
			_lineSprite.graphics.moveTo(prevX, prevY);
			_lineSprite.graphics.lineTo(x, y);

			bitmapData.draw(_lineSprite);

			smoothing = true;
		}

		public function clear() : void
		{
			bitmapData.fillRect(bitmapData.rect, 0x00000000);
		}

		public function lock() : void
		{
			bitmapData.lock();
		}

		public function unlock() : void
		{
			bitmapData.unlock();
		}

		public function addFilter(filter : *) : void
		{
			bitmapData.applyFilter(bitmapData, bitmapData.rect, _zeroPoint, filter);
		}

		public function addFilters(filters : Array) : void
		{
			for each (var filter : * in filters)
			{
				addFilter(filter);
			}
		}
	}
}
